import itertools
import random
import unittest

import bpy

from ..utils.log import log
from .utils import new_tag_empty, OBJECT_OUT, set_out_path, POOL_OUT, SCENE_OUT, random_transform, clear_scene
from ..utils.preferences import Preferences


class TestExportFile(Preferences,
                     unittest.TestCase,
                     ):
    def setUp(self):
        print('清理场景')
        clear_scene()

    def tearDown(self):
        clear_scene()

    def start_test(self):
        self.test_object_export()
        self.test_pool_export()
        self.test_scene_export()

    def sel_object(self, num):
        pref = self.pref
        items = set()
        a = 0
        while True:
            a += 1
            scene = pref.pop_object
            if scene not in items:
                items.add(scene)
                scene.selected = True
            if len(items) == num or (a > 99):
                break

        log.debug("selected_object")

    def sel_scene(self, num):
        pref = self.pref
        items = set()
        a = 0
        while True:
            a += 1
            scene = pref.pop_scene
            if scene not in items:
                items.add(scene)
                scene.selected = True
            if len(items) == num or (a > 99):
                break

        log.debug("selected_scene")

    def set_render_matching(self, obj: "bpy.tyeps.Object"):
        """设置渲染框的随机设置
        添加多个渲染设置
        """
        ls = (True, False)
        random_transform(obj)
        render_matching = obj.render_master.render_matching
        render_matching.clear()
        for index, (a, b, c) in enumerate(itertools.product(ls, ls, ls)):
            m = render_matching.add()
            m.name = str(index)

            m.use_assets_path = a
            m.search_children = b
            m.use_selected_asset = c

            m.assets_path = self.pref.pop_folder_object  # 资产路径
            m.assets_tree = self.pref.pop_folder_object  # 资产树
            m.selected_collect_name = self.pref.pop_select_collect.name  # 选择集名称
            m.render_matching_probability = random.randint(1, 100)

    def test_scene_export(self):

        pref = self.pref
        pref.mode = "SCENE"  # 设置为场景时将会导入一个新的场景,需要设置完成后再更改输出路径
        pref.asset_pointer.asset_items_pointer_.asset_select_collect_index = -1

        self.sel_object(10)

        scene = pref.pop_scene
        pref.export_assign_file_count = 10
        asset_items = pref.asset_pointer.asset_items
        asset_items.asset_scene_index = scene.index

        select_collect = asset_items.asset_select_collect
        select_collect.clear()

        # 先择集
        for i in range(5):  # 添加5个选择集
            sc = select_collect.add()
            sc.name = str(i)
            sc.set_as_active()
            for _ in range(10):  # 随机选择10个物体
                obj = pref.pop_object
                obj.selected = True
        for _ in range(3):
            self.set_render_matching(new_tag_empty())  # 再加两个空物体框用来匹配

        # 设置TAG

        set_out_path(SCENE_OUT)
        pref.asset_pointer.asset_items_pointer_.asset_select_collect_index = -1
        bpy.ops.asset.render_file_export(is_popup_menu=False)

    def test_object_export(self):
        """选择几个场景
        """
        set_out_path(OBJECT_OUT)
        self.pref.mode = "OBJECT"
        self.sel_scene(10)
        bpy.ops.asset.render_file_export(is_popup_menu=False)

    def test_pool_export(self):
        set_out_path(POOL_OUT)
        self.pref.mode = "POOL"
        self.sel_scene(5)
        self.sel_object(5)
        bpy.ops.asset.render_file_export(is_popup_menu=False)
